/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.POJO_ChuyenGa;
import POJO.POJO_ChuyenTau;
import POJO.POJO_GaTau;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Master
 */
public class DAO_ChuyenGa {
    DataProvider data;
    
    public List<POJO_ChuyenGa> LoadList() throws SQLException{
        data = new DataProvider();
        data.Open();
        
        String query = "SELECT * FROM chuyen_ga";
        List<POJO_ChuyenGa> list = new ArrayList<>();
        
        ResultSet rs = data.ExecuteQuery(query);
        try {
                 while (rs.next()) {
                    POJO_ChuyenGa cg = new POJO_ChuyenGa();
                    cg.setMACHUYEN(rs.getString("MACHUYEN"));
                    cg.setMAGA(rs.getString("MAGA"));
                    cg.setTRINHTU(rs.getInt("TRINHTU"));
                    cg.setTHOIGIANDEN(rs.getDate("THOIGIANDEN"));
                
                    list.add(cg); 
                 }
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        }
        
        return list;
    } 
    
    
    public int xoaChuyenGa(POJO_ChuyenGa cg) {
        data = new DataProvider();
        data.Open();
        String sql = String.format("DELETE FROM chuyen_ga WHERE MaChuyen='%s' and MaGa = '%s'",
                    cg.getMACHUYEN(),
                    cg.getMAGA());
            
        return data.ExeCuteUpdate(sql);
    }
    
    public int themChuyenGa(POJO_ChuyenGa cg) {
        data = new DataProvider();
        data.Open();
        
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            
        String sql = String.format("Insert into chuyen_ga values ('%s','%s',%d,'%s')",
                    cg.getMACHUYEN(),
                    cg.getMAGA(),
                    cg.getTRINHTU(),
                    cg.getTHOIGIANDEN());
         
        return data.ExeCuteUpdate(sql);
    } 
    
    public int capNhatChuyenGa(POJO_ChuyenGa cg)  {
        data = new DataProvider();
        data.Open();
        
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            
        String sql = String.format("UPDATE chuyen_ga SET TrinhTu = %d, ThoiGianDen = '%s' WHERE MaChuyen = '%s' and MaGa = '%s'",
                    cg.getTRINHTU(),
                    df.format(cg.getTHOIGIANDEN()),
                    cg.getMACHUYEN(),
                    cg.getMAGA());
        
        return data.ExeCuteUpdate(sql);
    }
}
